package android.pot.com.smartcar.ui.find;

import android.content.Context;
import android.os.Bundle;
import android.pot.com.smartcar.R;
import android.pot.com.smartcar.action.Action;
import android.pot.com.smartcar.base.BaseFragment;
import android.pot.com.smartcar.base.CommonAdapter;
import android.pot.com.smartcar.base.MyResponseHandler;
import android.pot.com.smartcar.base.ViewHolder;
import android.pot.com.smartcar.config.MyConfig;
import android.pot.com.smartcar.entity.AdListEntity;
import android.pot.com.smartcar.tool.LoadingTool;
import android.pot.com.smartcar.util.GsonUtil;
import android.pot.com.smartcar.util.TextCheckUtil;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhaoshilu on 2017/11/14.
 */
public class FindFragment extends BaseFragment {

    private static FindFragment instance;

    private View view;

    private Context context;

    private RefreshLayout refreshLayout;

    private boolean showLoading = true;

    private int curPage = 1;

    private int pageCount;

    private boolean needClear = true;

    private ListView listview;

    private CommonAdapter mAdapter;

    private List<AdListEntity> listModels = new ArrayList<>();

    private ArrayList<String> imgList = new ArrayList<String>();

    public static FindFragment instance() {
        if (instance == null) {
            instance = new FindFragment();
        }
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_find, container, false);
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        initView(view);
        addListener();
        initData();

        return view;

    }


    @Override
    protected void initView(View view) {
        listview = (ListView) view.findViewById(R.id.listview);
        initRefreshView();
        initRecommandView();
     }

    private void initRecommandView() {
        mAdapter = new CommonAdapter<AdListEntity>(context, listModels, R.layout.listcell_find) {
            @Override
            public void convert(ViewHolder helper, AdListEntity item) {

                helper.setImageByUrl(R.id.iv_icon, MyConfig.IMG_URL+TextCheckUtil.CheckString(item.getImg_url()));
            }
        };
        listview.setAdapter(mAdapter);
    }

    private void initRefreshView() {
        refreshLayout = (RefreshLayout) view.findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                needClear = true;
                showLoading = false;
                curPage = 1;
                requestData();

            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                curPage++;
                needClear = false;
                showLoading = false;

            }
        });


    }



    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }




    @Override
    protected void addListener() {
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bundle bundle = new Bundle();
                bundle.putString("img",imgList.get(i));
                startAct(context, PhotoActivity.class, bundle);
//                bundle.putStringArrayList("img", imgList);
//                bundle.putInt("CurPic",i);
//                bundle.putBoolean("single",true);
//                startAct(context, PhotosScanActivity.class, bundle);



            }
        });
    }


    @Override
    protected void initData() {
        requestData();


    }


    private void requestData(){
        Map<String,String> params = new HashMap<>();
        if (showLoading){
            LoadingTool.StartLoading(context);
        }

        Action.get().find_img(context, params, new MyResponseHandler() {
            @Override
            public void onSuccess(String message,JSONObject response) {
                if (showLoading) {
                    LoadingTool.EndLoading(context);
                }
                try {
                    JSONArray jsonArray = response.getJSONArray("data");
                    List<AdListEntity> listEntities = GsonUtil.getObjectList(jsonArray.toString(), AdListEntity.class);
                    if (needClear){
                        listModels.clear();
                        imgList.clear();
                    }
                    for (AdListEntity entity:listEntities){
                        imgList.add( MyConfig.IMG_URL+entity.getDetail_img());
                    }
                    listModels.addAll(listEntities);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                mAdapter.notifyDataSetChanged();
                refreshLayout.finishRefresh();
                refreshLayout.finishLoadMore();
                if (listModels.size() < pageCount) {
                    refreshLayout.setEnableLoadMore(true);
                } else {
                    refreshLayout.setEnableLoadMore(false);
                }
            }
        });
    }





}